package cn.ssm.confilg;

import cn.ssm.component.MyLocaleResolver;
import cn.ssm.controller.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MySpringMvcConfigurer {


    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            //添加视图控制
            /**
             * 配置视图控制器的映射
             *
             * 该方法用于向Spring MVC框架注册视图控制器的映射关系
             * 它定义了特定URL路径与视图名称之间的映射，以便当用户访问这些路径时，相应的视图会被渲染
             *
             * @param registry 视图控制器注册表，用于添加视图控制器的映射关系
             */
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                // 将根路径 "/" 映射到登录页面视图 "main/login"
                registry.addViewController("/").setViewName("main/login");

                // 将 "/index.html" 路径映射到登录页面视图 "main/login"
                registry.addViewController("/index.html").setViewName("main/login");

                // 将 "/main.html" 路径映射到主页面视图 "main/index"
                registry.addViewController("/main.html").setViewName("main/index");
            }
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry.addInterceptor(new LoginHandlerInterceptor())
                        //指定要拦截的请求 /** 表示拦截所有请求
                        .addPathPatterns("/**")
                        //排除不需要拦截的请求路径
                        .excludePathPatterns("/admin/captcha","/", "/index.html", "/login","/public.html","/public","/main.html","/usermm","/zczm","main/zc","main/lxwm","/lxwm.html","/lxwm")
                        //springboot2+之后需要将静态资源文件的访问路径 也排除
                        .excludePathPatterns("/css/*", "/img/*","/js/*");
            }
        };
    }
    // 配置一个自定义的区域解析器
    @Bean
    public LocaleResolver localeResolver() {
        // 返回一个新的自定义区域解析器实例
        return new MyLocaleResolver();
    }


}
